package com.yonyou.pmclouds.safetypatrol;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetypatrol.entity.ParentState;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolDetailQuery;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolQuery;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolService;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RequestMapping("/safetypatrol")
@RestController
public class SafetypatrolController {
    public static Logger logger = LoggerFactory.getLogger(SafetypatrolController.class);

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SafetypatrolQuery safetypatrolQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SafetypatrolDetailQuery safetypatrolDetailQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SafetypatrolService safetypatrolService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private PictureRemoteService pictureRemoteService;


//    首页展示接口
    @RequestMapping("/querybyname")
    public Map<String,Object> findByName(@RequestParam(required = false,defaultValue = "1")int pageIndex,
                                               @RequestParam(required = false,defaultValue = "10")int pageSize,
                                               @RequestParam(required = false,defaultValue = "2") int stateCode,
                                               @RequestParam(required = false)String keyword) throws BusinessException{
        int pageNum = pageIndex*pageSize-pageSize;


        Map<String, Object> pageMap = safetypatrolQuery.queryByCondition(keyword, pageNum, pageSize, (byte) stateCode);


        return pageMap;
    }

    //点进去展示主子表接口
    @RequestMapping("/querydetail")
    public SafetypatrolVO findDetail(String pk_safetypatrol)throws BusinessException {

        SafetypatrolVO safetypatrolVO =safetypatrolQuery.queryById(pk_safetypatrol);

        List<SafetypatrolDetailVO> safetypatrolDetailVOList =safetypatrolDetailQuery.queryById(pk_safetypatrol);

        if (safetypatrolDetailVOList!=null){
            BeanHelper.setProperty(safetypatrolVO, "safeDetailVOS", safetypatrolDetailVOList.toArray(new SafetypatrolDetailVO[safetypatrolDetailVOList.size()]));
            logger.info("============controller查询的SafetypatrolVO数据为{}=====================",safetypatrolVO);
        }

        return safetypatrolVO;
    }

    //首页中创建按钮 执行添加操作 校验放在前端
    @Insert
    @PMApprove
    @RequestMapping("/add")
    public SafetypatrolVO insertSafetypatrolVO(@RequestBody @Validated SafetypatrolVO safetypatrolVO, BindingResult bindingResult) throws BusinessException{
        if (bindingResult.hasErrors()){
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }

        //插入操作设置关联的企业id(客户id)
        safetypatrolVO.setPkTenant(RuntimeEnvironment.getTenantId());
        //设置状态 不过新增默认的启用可以不用添加 额看了没有设置默认
        safetypatrolVO.setStateCode(ParentState.ENABLE);

        SafetypatrolVO safetypatroldealwith =safetypatrolService.insert(safetypatrolVO);
        return safetypatroldealwith;
    }

    //首页删除
    @RequestMapping("/delete")
    public void delete(@RequestBody @Validated SafetypatrolVO safetypatrolVO,BindingResult bindingResult) throws BusinessException {
        if (bindingResult.hasErrors()){
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        safetypatrolService.delete(safetypatrolVO);
    }

    //首页编辑
    @PMApprove(trigger = "commit")
    @RequestMapping("/update")
    public SafetypatrolVO update(@RequestBody @Validated SafetypatrolVO safetypatrolVO,BindingResult bindingResult) throws BusinessException {
        if (bindingResult.hasErrors()){
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        SafetypatrolVO update = safetypatrolService.update(safetypatrolVO);
        return update;
    }

    //首页启动按钮
    @RequestMapping("/enable")
    public SafetypatrolVO enable(@RequestBody SafetypatrolVO safetypatrolVO) throws BusinessException {
        SafetypatrolVO enablestate =safetypatrolService.enable(safetypatrolVO);
        return enablestate;
    }

    //首页停用按钮
    @RequestMapping("/disable")
    public SafetypatrolVO disable(@RequestBody SafetypatrolVO safetypatrolVO) throws BusinessException {
        return safetypatrolService.disable(safetypatrolVO);
    }

    //两个页面删除前判断是否被引用的判断条件
    @RequestMapping("/referCheck")
    public boolean referCheck(@RequestBody SafetypatrolVO safetypatrolVO) throws BusinessException{
       return safetypatrolService.referCheck(safetypatrolVO);
    }

    //查看示例图片功能 这个写好的直接调用即可
    @RequestMapping("/getExamplePicVos")
    public PictureVO[] getExamplePicVos(@RequestParam("pkDetail")String pkDetail) throws BusinessException{
        return pictureRemoteService.queryExamplePicsByEntity(pkDetail);
    }




}
